package Test;

import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

public class ByteArrayTest {
    public static void main(String[] args) throws Exception {
        String str="1111";
// Unicode的编码就是UTF-16，每个字符占两个字节，UTF-8每个字符占3个字节
        ByteArrayInputStream bais = new ByteArrayInputStream( str.getBytes("UTF-16"));
        int len=bais.available( );

        System.out.println( len);
        byte[] testread =new byte[6];bais.read(testread );

        ByteBuffer bb = ByteBuffer.allocate(testread.length);
        bb.put(testread);
        bb.flip( );

        Charset cs = Charset.forName("UTF-16");
        CharBuffer cb = cs.decode(bb);
        char[] chs=cb.array();

        for (int i=i=0;i<chs.length;i++){
            System.out.println(chs[i]);
        }
        if (chs[chs.length-1]=='\0')
            System.out.println("最后一个字符表示空");
        else
            System.out.println("最后一个字符非空");


    }
}
